using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Tranquillity;
using Tranquillity.Helpers;

namespace GalaxyDefenderChaos
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game, MainMenuListener, OptionMenuListener, BackToMainMenuListener, EndGameListener
    {
        Configuration _Configuration;
        SoundCenter _SoundCenter;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        /*
        private PerspectiveCamera _Camera;
        */

        Camera _Camera;

        private My2DMainMenu _MainMenu;
        private Stage _Stage;
        private My2DOptionMenu _OptionMenu;

        private VisibleGameEntity _CurrentScene;
        private My2DAboutMenu _AboutMenu;
        private BillBoard _BillBoard;
        private EndGameScene _EndGameScene;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);

            graphics.IsFullScreen = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            
            //_Camera = new PerspectiveCamera(new Vector3(0, 0, 1000), new Vector3(0, 0, 0), Vector3.Up, 1f, 1000000f, MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio);


            _Camera = new ChaseCamera(new Vector3(0, 400, 1500),
                    new Vector3(0, 200, 0),//y=200
                    new Vector3(0, 0, 0), GraphicsDevice);

            _Configuration = new Configuration();
            _SoundCenter = new SoundCenter();

           

            _MainMenu = new My2DMainMenu(Content, GraphicsDevice, _SoundCenter);
            _MainMenu.AddListener(this);

           
            //_Stage = new Stage(Content, GraphicsDevice, _Camera, _SoundCenter, graphics, this);
            //_Stage.AddEndGameListener(this);

            _OptionMenu = new My2DOptionMenu(Content, GraphicsDevice);
            _OptionMenu.AddListener(this);

            _AboutMenu = new My2DAboutMenu(Content, GraphicsDevice);
            _AboutMenu.AddListener(this);

            _EndGameScene = new EndGameScene(Content, GraphicsDevice);
            _EndGameScene.AddListener(this);

            _CurrentScene = _MainMenu;
            _MainMenu.PlaySound();

            //_BillBoard = new BillBoard(Content, "Bar/Bar", new Vector2(1000, 1000), Vector3.Zero, Vector3.Up, GraphicsDevice, _Camera);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            _Camera.Update();
            _CurrentScene.Update(gameTime, TouchPanel.GetState());
            //_BillBoard.Update(gameTime, TouchPanel.GetState());
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            _CurrentScene.Draw(GraphicsDevice, _Camera, gameTime);
            //_BillBoard.Draw(GraphicsDevice, _Camera, gameTime);
            //DrawDebugInfo();
            base.Draw(gameTime);
        }

        void DrawDebugInfo()
        {
            SpriteFont spriteFont = Content.Load<SpriteFont>("hudFont");
            spriteBatch.Begin();

            spriteBatch.DrawString(spriteFont, (_Camera.CameraPosition.X + " " + _Camera.CameraPosition.Y + " " + _Camera.CameraPosition.Z
                + "\n" + _Camera.CameraTarget.X + " " + _Camera.CameraTarget.Y + " " + _Camera.CameraTarget.Z
                ), new Vector2(50, 50), Color.Red);

            spriteBatch.End();
        }

        public void NewGameSelected() 
        {
            _Stage = new Stage(Content, GraphicsDevice, _Camera, _SoundCenter, graphics, this, _Configuration);
            _Stage.AddEndGameListener(this);
            _Stage.AddBackToMainMenuListener(this);
            _CurrentScene = _Stage;
            _Stage.PlaySound();
        }

        public void OptionSelected()
        {
            _CurrentScene = _OptionMenu;
        }

        public void AboutSelected()
        {
            _CurrentScene = _AboutMenu;
        }

        public void ExitSelected()
        {
            Exit();
        }

        public void SoundOnSelected()
        {
            _Configuration.IsSoundOn = true;
            _SoundCenter.IsSoundOn = true;
        }

        public void SoundOffSelected()
        {
            _Configuration.IsSoundOn = false;
            _SoundCenter.IsSoundOn = false;
        }

        public void NormalLevelSelected()
        {
            _Configuration.DifficutyLevel = 0;
        }

        public void HardLevelSelected()
        {
            _Configuration.DifficutyLevel = 1;
        }

        public void BackToMainMenuSelected()
        {
            _CurrentScene = _MainMenu;
        }

        public void YouWin()
        {
            _Stage.StopSound();
            _EndGameScene.IsWin = true;
            _CurrentScene = _EndGameScene;
        }

        public void YouLose()
        {
            _Stage.StopSound();
            _EndGameScene.IsWin = false;
            _CurrentScene = _EndGameScene;
        }
    }
}
